﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Configuration;
using System.Runtime.InteropServices;

namespace CoreShop.App
{
    public class httpDown
    {
        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key,
            string def, StringBuilder retVal, int size, string filePath);

        public delegate void InitalBar(long size);
        public event InitalBar initalProgressbar;

        public delegate void SetBarProgress(long size);
        public event SetBarProgress setBarprogress;

        public delegate void setLog(string msg);
        public event setLog SetLog;

        public bool HttpDownload(string downloadUrl, string localpath,int mFileIndex)
        {
            Stream responseStream = null;
            FileStream fileStream = null;
            StreamReader reader = null;
            bool result = false;
            int dledKbs = 0;

            try
            {

                WebRequest webReq = WebRequest.Create(downloadUrl);
                WebResponse webRes = webReq.GetResponse();
                long size = webRes.ContentLength;

                responseStream = webRes.GetResponseStream();

                string fileName = Path.GetFileName(webReq.RequestUri.LocalPath);
                string ToUrl = localpath + "\\" + fileName;
                if (initalProgressbar != null) initalProgressbar(size);

                string Dir = System.IO.Path.GetDirectoryName(ToUrl);

                if (!Directory.Exists(Dir))
                {
                    Directory.CreateDirectory(Dir);
                }

                if (fileName.Length == 0)
                {
                    reader = new StreamReader(responseStream);
                    //Console.WriteLine(reader.ReadToEnd());
                }
                else
                {
                    fileStream = new FileStream(ToUrl, FileMode.Create);

                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while (true)
                    {
                        bytesRead = responseStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0) break;
                        fileStream.Write(buffer, 0, bytesRead);
                        dledKbs++;
                        if (setBarprogress != null) setBarprogress(dledKbs * 1024);
                    }
                }
                if (setBarprogress != null) setBarprogress(size* 1024);
                if (SetLog != null) SetLog(DateTime.Now.ToString() + string.Format("  第{0}个文件下载完成;\r\n", mFileIndex));
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
                throw new Exception(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                else if (responseStream != null)
                    responseStream.Close();
                if (fileStream != null)
                    fileStream.Close();
            }
            return result;
        }

    }
}
